package com.logic.modular.game.battle.process.battleStage.actionPhase.actionPhase1;

import com.logic.comment.util.BeanUtil;
import com.logic.modular.game.battle.panel.Battle;
import com.logic.modular.game.battle.panel.attribute.PlayerHeroAttributeServiceImpl;
import com.logic.modular.game.battle.panel.attribute.domain.PlayerHeroAttribute;
import com.logic.modular.game.battle.panel.skillEffect.PvpSkillEffect;
import com.logic.modular.game.battle.panel.skillEffect.PvpSkillEffectServiceImpl;
import com.logic.modular.game.battle.process.domain.EffectTriggerInfo;
import com.logic.modular.game.battle.process.domain.PreActionInfo;

import com.logic.modular.game.battle.process.skillEffectHandle.skillEffectSelect.SkillEffectTriggerHandle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 1
 * 行动前准备
 */
@Slf4j
@Service
public class PreparationBeforeActionService {
    @Resource
    private PvpSkillEffectServiceImpl skillEffectService;
    @Resource
    private PlayerHeroAttributeServiceImpl attributeService;

    public PreActionInfo apply(String battleUUID, PlayerHeroAttribute actWJ) {
        log.info("==================武将行动前阶段,玩家{}武将{}({})兵力{}",
                actWJ.getUid(), actWJ.getName(), actWJ.getId(), actWJ.getCurrentForce());
        Battle battle = Battle.get(battleUUID);
        Long battleId = battle.getBattleId();


        PreActionInfo preActionInfo = new PreActionInfo();

        List<PlayerHeroAttribute> playerHeroAttributes = attributeService.listCache(battleUUID);

        for (PlayerHeroAttribute playerHeroAttribute : playerHeroAttributes) {
            playerHeroAttribute.refreshRoundInfo();
            playerHeroAttribute.clearAction();
            attributeService.saveCache(playerHeroAttribute);
        }
        PlayerHeroAttribute playerHeroAttribute = attributeService.get(battleUUID, actWJ.getId());
        playerHeroAttribute.setPhase(21);
        attributeService.saveCache(playerHeroAttribute);

        Map<String, List<PvpSkillEffect>> collect = skillEffectService.list(battleId).stream().filter(
                skillEffect -> new BeanUtil.Selector()
                        .eq(actWJ.getId(), skillEffect.getTargetPlayerHeroId())
                        .eq(21, skillEffect.getBaseTrigger().getTrigger3())
                        .value()
        ).collect(Collectors.groupingBy(p ->
                "来源武将:" + p.getSourcePlayerHeroId() + "-"
                        + "技能id:" + p.getSourceCoreSkillId() + "-"
                        + "技能星阶id:" + p.getSourceCoreSkillStarId() + "-"
                        + "技能名称:" + p.getCoreSkillName() + "-"
                        + "[技能描述:" + p.getCoreSkillInfo() + "]"
        ));
        collect.forEach((k, v) -> {
            log.info(k);
            List<PvpSkillEffect> pvpSkillEffects = SkillEffectTriggerHandle.applyNewSkillEffect(v);
            List<EffectTriggerInfo> skillTriggerInfoList = pvpSkillEffects.stream()
                    .map(PvpSkillEffect::getSkillEffectTriggerInfo)
                    .collect(Collectors.toList());
            preActionInfo.skillTriggerInfoList.addAll(skillTriggerInfoList);
        });
        log.info("行动前准备信息{}", preActionInfo.skillTriggerInfoList);
        return preActionInfo;
    }
}
